package com.example.black.myapplication.myhandler;

public class MyMessage {
    public int arg1;
    public int arg2;
    public MyHandler target;
    public Object obj;
    public int what;
    private static Object sync = new Object();
    private static MyMessage sPool;
    public MyMessage next;
    public Runnable callBack;
    private static final int MAX_POOL_SIZE = 50;
    private static int sPoolSize = 0;
    //没有考虑线程安全
//    public static MyMessage obtain(){
//        if (0 >= sPoolSize){
//            return new MyMessage();
//        }
//        MyMessage msg = sPool;
//        sPool = msg.next;
//        sPoolSize --;
//        return msg;
//    }

    public static MyMessage obtain(){
        synchronized (sync){
            if (sPool != null){
                MyMessage msg  =sPool;
                sPool = msg.next;
                sPoolSize --;
                return msg;
            }
        }
        return new MyMessage();
    }
    public void recycle() {
        arg1 = 0;
        arg2 = 0;
        target = null;
        obj = null;
        if (MAX_POOL_SIZE > sPoolSize){
            this.next = sPool;
            sPool = this;
            sPoolSize++;
        }
    }
}
